<template>
    <div class="app-container">
        <div class="search-container">
            <el-form v-show="showSearch" ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px">
                <el-form-item label="执行频率描述" prop="freqDesc">
                    <el-input v-model="queryParams.freqDesc" clearable placeholder="请输入执行频率描述"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="医嘱类型" prop="dictType">
                    <el-select v-model="queryParams.dictType" class="width-120" clearable placeholder="请选择医嘱类型">
                        <el-option v-for="dict in dictDataMap['order_type']" :key="dict.dictValue"
                            :label="dict.dictLabel" :value="dict.dictValue" />
                    </el-select>
                </el-form-item>
                <div style="float: right" class="mr20">
                    <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery" />
                </div>
            </el-form>
        </div>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button v-hasPerm="['base:performFreqDict:add']" type="primary" @click="handleAdd">
                    <template #icon>
                        <i-ep-plus />
                    </template>
                    新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button v-hasPerm="['base:performFreqDict:edit']" :disabled="single" plain type="success"
                    @click="handleUpdate">
                    <template #icon>
                        <i-ep-edit />
                    </template>
                    修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button v-hasPerm="['base:performFreqDict:remove']" :disabled="multiple" plain type="danger"
                    @click="handleDelete">
                    <template #icon>
                        <i-ep-delete />
                    </template>
                    删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button v-hasPerm="['base:performFreqDict:export']" plain type="warning" @click="handleExport">
                    <template #icon>
                        <i-ep-download />
                    </template>
                    导出
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button plain type="danger" @click="refreshCachePerformFreqDictFun">
                    <template #icon>
                        <i-ep-refresh />
                    </template>
                    刷新缓存
                </el-button>
            </el-col>
        </el-row>

        <el-table v-adaptive :data="performFreqDictList" :border="true" :stripe="true" highlight-current-row
            @selection-change="handleSelectionChange">
            <el-table-column :align="'center'" type="selection" width="55" />
            <el-table-column :align="'center'" label="执行频率描述" prop="freqDesc" />
            <el-table-column :align="'center'" label="频率次数" prop="freqCounter" />
            <el-table-column :align="'center'" label="频次间隔" prop="freqInterval" />
            <el-table-column :align="'center'" label="频次单位" prop="freqIntervalUnit" />
            <el-table-column :show-overflow-tooltip="true" :align="'center'" label="执行时间" prop="performTime" />
            <el-table-column :align="'center'" label="医嘱类型" prop="dictType" width="100">
                <template #default="scope">
                    <dict-data-tag :options="dictDataMap['order_type']" :value="scope.row.dictType" />
                </template>
            </el-table-column>
            <el-table-column :align="'center'" label="显示顺序" prop="showSort" width="70" />
            <el-table-column :align="'center'" label="备注" prop="remark" />
            <el-table-column :align="'center'" class-name="small-padding fixed-width" label="操作">
                <template #default="scope">
                    <el-button v-hasPerm="['base:performFreqDict:edit']" type="success"
                        @click="handleUpdate(scope.row)">
                        <template #icon>
                            <i-ep-edit />
                        </template>
                        修改
                    </el-button>
                    <el-button v-hasPerm="['base:performFreqDict:remove']" type="danger"
                        @click="handleDelete(scope.row)">
                        <template #icon>
                            <i-ep-download />
                        </template>
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
            :total="total" @pagination="getList" />
        <!-- 添加或修改角色信息对话框 -->
        <el-dialog :close-on-click-modal="false" :title="title" v-model="open" append-to-body width="40%">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
                <el-row :gutter="40">
                    <el-col :span="12">
                        <el-form-item label="执行频率描述" prop="freqDesc">
                            <el-input v-model="form.freqDesc" placeholder="请输入执行频率描述" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="频率次数" prop="freqCounter">
                            <el-input v-model="form.freqCounter" placeholder="请输入频率次数" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="频次间隔" prop="freqInterval">
                            <el-input v-model="form.freqInterval" placeholder="请输入频次间隔" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="频次单位" prop="freqIntervalUnit">
                            <el-input v-model="form.freqIntervalUnit" placeholder="请输入频次单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="执行时间" prop="performTime">
                            <el-input v-model="form.performTime" placeholder="请输入频次单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="医嘱类型">
                            <el-radio-group v-model="form.dictType">
                                <el-radio v-for="dict in dictDataMap['order_type']" :key="dict.dictValue"
                                    :value="dict.dictValue">{{
                                        dict.dictLabel
                                    }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="显示顺序" prop="showSort">
                            <el-input v-model="form.showSort" placeholder="请输入显示顺序" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" placeholder="请输入内容" type="textarea" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup>
import {
    addPerformFreqDict,
    delPerformFreqDict,
    getPerformFreqDict,
    refreshCachePerformFreqDict,
    selectPagePerformFreqDict,
    updatePerformFreqDict
} from "@/api/base/performFreqDict";
import { useDictStoreHook } from "@/store/modules/dict";

const formRef = ref()
const dictStore = useDictStoreHook();
const dictDataMap = ref(dictStore.isTypeGetData('order_type'))

// 按钮loading
const buttonLoading = ref(false)
// 选中数组
const ids = ref([])
// 非单个禁用
const single = ref(true)
// 非多个禁用
const multiple = ref(true)
// 显示搜索条件
const showSearch = ref(true)
// 总条数
const total = ref(0)
// 执行频次表格数据
const performFreqDictList = ref([])
// 弹出层标题
const title = ref("")
// 是否显示弹出层
const open = ref(false)
// 查询参数
const queryParamsRef = ref()
const queryParams = ref<any>({
    pageNum: 1,
    pageSize: 10,
    freqDesc: undefined,
    freqCounter: undefined,
    showSort: undefined,
    freqInterval: undefined,
    freqIntervalUnit: undefined,
    dictType: undefined,
})
// 表单参数
const form = ref<any>({})
// 表单校验
const rules = ref({
    freqId: [
        { required: true, message: "执行频次主键不能为空", trigger: "blur" }
    ],
    freqDesc: [
        { required: true, message: "执行频率描述不能为空", trigger: "blur" }
    ],
    freqCounter: [
        { required: true, message: "频率次数不能为空", trigger: "blur" }
    ],
    showSort: [
        { required: true, message: "显示顺序不能为空", trigger: "blur" }
    ],
    freqInterval: [
        { required: true, message: "频次间隔不能为空", trigger: "blur" }
    ],
    // freqIntervalUnit: [
    //     {required: true, message: "频次单位不能为空", trigger: "blur"}
    // ],
    dictType: [
        { required: true, message: "0:长期医嘱 1:临时医嘱不能为空", trigger: "blur" }
    ],
})

getList();

function refreshCachePerformFreqDictFun() {
    refreshCachePerformFreqDict().then((res: any) => {
        ElMessage.success(res.msg);
    });
}

/** 查询执行频次列表 */
function getList() {
    selectPagePerformFreqDict(queryParams.value).then((response: any) => {
        performFreqDictList.value = response.rows;
        total.value = response.total;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        freqId: undefined,
        freqDesc: undefined,
        freqCounter: undefined,
        showSort: undefined,
        freqInterval: undefined,
        freqIntervalUnit: undefined,
        performTime: undefined,
        dictType: "0",
        remark: undefined
    };
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    queryParamsRef.value.resetFields()
    queryParams.value.freqDesc = ''
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.freqId)
    single.value = selection.length !== 1
    multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加执行频次信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const freqId = row.freqId || ids.value
    getPerformFreqDict(freqId).then((response: any) => {
        open.value = true;
        form.value = response.data;
        title.value = "修改执行频次信息";
    });
}

/** 提交按钮 */
function submitForm() {
    (formRef.value as any).validate(valid => {
        if (valid) {
            buttonLoading.value = true;
            if (form.value.freqId != null) {
                updatePerformFreqDict(form.value).then(_response => {
                    ElMessage.success("修改成功");
                    open.value = false;
                    getList();
                }).finally(() => {
                    buttonLoading.value = false;
                });
            } else {
                addPerformFreqDict(form.value).then(_response => {
                    ElMessage.success("新增成功");
                    open.value = false;
                    getList();
                }).finally(() => {
                    buttonLoading.value = false;
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const freqIds = row.freqId || ids.value;
    ElMessageBox.confirm('是否确认删除执行频次信息编号为"' + freqIds + '"的数据项？',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        delPerformFreqDict(freqIds).then(() => {
            getList();
            ElMessage.success("删除成功");
        })
    }).catch(() => {
    })
}

/** 导出按钮操作 */
function handleExport() {
    // this.download('admin/performFreqDict/export', {
    //     ...this.queryParams
    // }, `performFreqDict_${new Date().getTime()}.xlsx`)
}
</script>
<script lang="ts">
export default {
    name: "PerformFreqDict"
}
</script>
